package com.yhm.mybatis.bean;

import com.yhm.mybatis.Common;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import org.springframework.jdbc.datasource.lookup.DataSourceLookup;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class YhmAbstractRoutingDataSource extends AbstractRoutingDataSource {

  private Object writeDataSource;
  private Object readDataSource;

  public Object getWriteDataSource() {
    return writeDataSource;
  }

  public void setWriteDataSource(Object writeDataSource) {
    this.writeDataSource = writeDataSource;
  }

  public Object getReadDataSource() {
    return readDataSource;
  }

  public void setReadDataSource(Object readDataSource) {
    this.readDataSource = readDataSource;
  }

  @Override
  public void afterPropertiesSet() {

    setDefaultTargetDataSource(readDataSource);

    Map<Object, Object> targetDataSources = new HashMap<>();
    targetDataSources.put("read", readDataSource);
    targetDataSources.put("write", writeDataSource);
    this.setTargetDataSources(targetDataSources);
    super.afterPropertiesSet();
  }

  @Override
  protected Object determineCurrentLookupKey() {
    return null;
  }


}
